// pages/settleIn/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name:'',
    price:'',
    costPrice:'',
    type:'分类错误会影响审核结果，请仔细确认',
    latitude:'',
    address:'',
    userName:'',
    mobile:'',
    columns: [],
    tabList:['不共享','共享'],
    ggList:[],
    guige:'',
    isPopup:false,
    isTip:false,
    typeId:'',
    image:'https://huitongkeji.oss-cn-shenzhen.aliyuncs.com/1715581641832.png',
    detailImage:'https://huitongkeji.oss-cn-shenzhen.aliyuncs.com/1715581601981.png',
    smallImage:'https://huitongkeji.oss-cn-shenzhen.aliyuncs.com/1715581524442.png',
    id:''
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options.id)
    this.setData({
      id:options.id
    })
    this.typeList()
    
  },
  proUpdDetails(){
    let data = {
      id:this.data.id,
      storeId:wx.getStorageSync('userInfo').storeId
    }
    app.agriknow.proUpdDetails(data).then(res=>{
      console.log(res)
      // 分类转换
      this.data.typeList.forEach((item,index)=>{
        if(item.id==res.data.typeId){
          this.setData({
            type:item.name
          })
        }
      })
      console.log(this.data.type,res.data.typeId)
      this.setData({
        name:res.data.name,
        price:res.data.price,
        costPrice:res.data.costPrice,
        typeId:res.data.typeId,
        isShare:res.data.isShare,
        tabIndex:res.data.isShare,
        ggList:res.data.specifications.split(','),
        image:res.data.image,
        smallImage:res.data.smallImage,
        detailImage:res.data.detailImage
      })
    })
  },
  tipClick(e){
    console.log(e)
    let index = e.currentTarget.dataset.index
    this.setData({
      tabIndex:index
    })
  },
  timeClick(e){
    let index = e.currentTarget.dataset.index
    let list = this.data.timeList
    list[index].status = !list[index].status
    this.setData({
      timeList:list
    })
  },
  opentip(){
    this.setData({
      isTip:true
    })
  },
  isOk(){
    this.setData({
      isTip:false
    })
  },
  typeList(){
    let data = {
      id:wx.getStorageSync('userInfo').storeId
    }
    app.agriknow.storeProTypeList(data).then(res=>{
      console.log(res)
      let columns = []
      res.data.forEach((item,index)=>{
        columns.push(item.name)
      })
      this.setData({
        typeList:res.data,
        columns:columns
      })
      this.proUpdDetails()
    })
  },
  submit(){
    wx.showLoading({
      title: '上传中',
      mask:true
    })
    if(!this.data.name) return this.tip('请输入店铺名称');
    if(!this.data.price) return this.tip('请输入产品价格');
    if(!this.data.costPrice) return this.tip('请输入供应价格');
    if(this.data.tabIndex==-1) return this.tip('请选择是否对外共享产品');
    if(!this.data.typeId) return this.tip('请选择分类');
    if(this.data.ggList.length==0) return this.tip('请添加规格');
    if(this.data.smallImage=='https://huitongkeji.oss-cn-shenzhen.aliyuncs.com/1715581524442.png') return this.tip('请上传缩略图');
    if(this.data.image=='https://huitongkeji.oss-cn-shenzhen.aliyuncs.com/1715581641832.png') return this.tip('请上传产品主图');
    if(this.data.detailImage=='https://huitongkeji.oss-cn-shenzhen.aliyuncs.com/1715581601981.png') return this.tip('请上传产品详情');
    let data = {
      name:this.data.name,
      typeId:this.data.typeId,
      image:this.data.image,
      storeImage:this.data.storeImage,
      detailImage:this.data.detailImage,
      smallImage:this.data.smallImage,
      price:this.data.price,
      costPrice:this.data.costPrice,
      isShare:this.data.tabIndex,
      specifications:this.data.ggList.join(','),
      storeId:wx.getStorageSync('userInfo').storeId,
      id:this.data.id
    }
    app.agriknow.storeUpdPro(data).then(res=>{
      console.log(res)
      wx.hideLoading()
      if(res.code=='000000'){
        wx.showModal({
          title: '提示',
          content: '提交成功',
          showCancel:false,
          success (res) {
            if (res.confirm) {
              console.log('用户点击确定')
              wx.reLaunch({
                url: '/pages/productManagement/index'
              })
            }
          }
        })
      }
    }).catch(res=>{
      wx.hideLoading()
      wx.showToast({
        title: res.message,
        icon: 'none',
        duration: 2000,
        mask: true
      })
    })
  },
  addGg(){
    let ggList = this.data.ggList
    if(this.data.guige){
      ggList.push(this.data.guige)
      this.setData({
        ggList:ggList,
        guige:''
      })
    }else{
      this.tip('请输入规格')
    }
    
  },
  upload(e) {
    let that = this
    let index = e.currentTarget.dataset.index
    console.log(index)
    wx.chooseMedia({
      count:1,
      success (res) {
        console.log(res.tempFiles[0].tempFilePath)
        const tempFilePaths = res.tempFiles[0].tempFilePath
        wx.uploadFile({
          url: app.agriknow._baseUrl+'ht/user/uploadImage', //仅为示例，非真实的接口地址
          filePath: tempFilePaths,
          name: 'file',
          formData: {
            'user': 'test'
          },
          success (res){
            console.log(res)
            if(index==1){
              that.setData({
                smallImage:JSON.parse(res.data).data
              })
            }else if(index==2){
              that.setData({
                image:JSON.parse(res.data).data
              })
            }else if(index==3){
              that.setData({
                detailImage:JSON.parse(res.data).data
              })
            }
            
          }
        })
      }
    })
  },
  onCancel(e){
    console.log(e)
    this.setData({
      isPopup:false
    })
  },
  onConfirm(e){
    console.log('1',e.detail.index)
    this.setData({
      isPopup:false,
      type:e.detail.value,
      typeId:this.data.typeList[e.detail.index].id
    })
  },
  openPopup(){
    this.setData({
      isPopup:true
    })
  },
  tip(name){
    wx.showToast({
      title: name,
      icon:'none',
      duration: 2000
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})